<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">景区名</label>
        <el-input v-model="query.name" clearable placeholder="景区名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">城市</label>
        <el-input v-model="query.city" clearable placeholder="城市" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="50%">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="景区名" prop="name">
            <el-input v-model="form.name" style="" />
          </el-form-item>
          <el-form-item label="城市" prop="city">
            <el-input v-model="form.city" style="" />
          </el-form-item>
          <el-form-item label="预览图">
            <!-- <el-input v-model="form.url" style="" /> -->
            <UploadFile v-model="form.url" @updateImg="updateImg" />

          </el-form-item>
          <el-form-item label="详细地址" prop="address">
            <el-input v-model="form.address" style="" />
          </el-form-item>
          <el-form-item label="价格" prop="price">
            <el-input v-model="form.price" style="" />
          </el-form-item>
          <el-form-item label="描述" prop="describe">
            <el-input v-model="form.describe" style="" type="textarea" />
          </el-form-item>
          <el-form-item label="门票数量" prop="number">
            <el-input v-model="form.number" style="" />
          </el-form-item>
          <el-form-item label="门票类型" prop="type">
            <el-radio-group v-model="form.type">
              <!-- <el-radio :label="是">是</el-radio> -->
              <!-- <el-radio :label="否">否</el-radio> -->
              <el-radio :label="'一日游'">一日游</el-radio>
              <el-radio :label="'周末游'">周末游</el-radio>
              <el-radio :label="'自由行'">自由行</el-radio>
              <el-radio :label="'跟团游'">跟团游</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="类型">
            <el-radio-group v-model="form.activityType">
              <el-radio :label="'原价'">原价</el-radio>
              <el-radio :label="'秒杀'">秒杀 </el-radio>
              <el-radio :label="'拼团'">拼团 </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="活动开始时间">
            <!-- <el-input v-model="form.startTime" style="" /> -->
            <el-date-picker v-model="form.startTime" type="datetime" />
          </el-form-item>
          <el-form-item label="截止时间">
            <!-- <el-input v-model="form.endTime" style="" /> -->
            <el-date-picker v-model="form.endTime" type="datetime" />
          </el-form-item>
          <el-form-item label="到达时间，秒杀场景必填">
            <!-- <el-input v-model="form.arriveTime" style="" /> -->
            <el-date-picker v-model="form.arriveTime" type="datetime" />

          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />

        <el-table-column prop="name" label="景区名" />
        <el-table-column prop="city" label="城市" />
        <!-- <el-table-column prop="url" label="预览图" /> -->
        <el-table-column prop="url" label="封面">
          <template slot-scope="{row}">
            <div v-if="row.url && row.url.length" style="width: 60px; height: 60px" @click.stop>
              <el-image
                v-if="row.url"
                :key="row.id"
                :src="row.url"
                :preview-src-list="[row.url]"
                fit="fill"
              >
                <div slot="error" class="image-slot">
                  <el-image :src="row.url" fit="cover" style="width: 60px; height: 60px" />
                </div>
              </el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="详细地址" />
        <el-table-column prop="price" label="价格" />
        <el-table-column prop="describe" label="描述" />
        <el-table-column prop="busnessName" label="商家名" />
        <el-table-column prop="number" label="门票数量" />
        <el-table-column prop="type" label="门票类型" />
        <el-table-column prop="activityType" label="类型" />
        <el-table-column prop="startTime" label="活动开始时间" />
        <el-table-column prop="endTime" label="活动截止时间" />
        <el-table-column prop="arriveTime" label="到达时间，秒杀场景" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="modifyTime" label="修改时间" />
        <el-table-column v-if="checkPer(['admin','scenicSpot:edit','scenicSpot:del'])" label="操作" width="250px" align="center">
          <template slot-scope="scope">
            <!-- <udOperation
              :data="scope.row"
              :permission="permission"
            /> -->
            <el-button
              slot="left"
              size="mini"
              type="success"
              icon="el-icon-s-comment"
              @click="open('scneReply',scope.row.id,scope.row.name+'-评论')"
            >查看评论</el-button>
            <el-button
              slot="left"
              size="mini"
              type="warning"
              icon="el-icon-sell"
              @click="open('scneOrder',scope.row.id,scope.row.name+'-订单')"
            >查看订单</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="80%"
    >
      <!-- <hotelOrder v-if="type==='room'" :key="timer" :hotel-id="row" />
        <roomIndex :key="timer" :hotel-id="row" />
        <hotelReply :key="timer" :hotel-id="row" /> -->
      <component :is="type" v-if="type" :key="timer" :scenic-id="row" />
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import crudScenicSpot from '@/api/scenicSpot'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import moment from 'moment'
import { mapGetters } from 'vuex'
import UploadFile from '@/components/UploadFile/index'
import scneOrder from './scneOrder.vue'
import scneReply from './scneReply.vue'

const defaultForm = { id: null, name: null, city: null, url: null, address: null, price: null, describe: null, busnessId: null, busnessName: null, number: null, type: null, createTime: null, modifyTime: null, activityType: null, startTime: null, endTime: null, arriveTime: null }
export default {
  name: 'ScenicSpot',
  components: { pagination, crudOperation, rrOperation, udOperation, UploadFile, scneOrder, scneReply },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '景点', url: 'api/scenicSpot', idField: 'id', sort: 'id,desc', crudMethod: { ...crudScenicSpot }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'scenicSpot:add'],
        edit: ['admin', 'scenicSpot:edit'],
        del: ['admin', 'scenicSpot:del']
      },
      rules: {
        name: [
          { required: true, message: '景区名不能为空', trigger: 'blur' }
        ],
        city: [
          { required: true, message: '城市不能为空', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '详细地址不能为空', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '价格不能为空', trigger: 'blur' }
        ],
        describe: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ],
        number: [
          { required: true, message: '门票数量不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'name', display_name: '景区名' }
      ],
      timer: null,
      dialogVisible: false,
      row: null,
      type: ''
    }
  },
  computed: {
    ...mapGetters(['user'])

  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      const isAdmin = this.user.roles.find(el => el.id == 1)
      if (!isAdmin) {
        this.query.busnessId = this.user.id
      }
      return true
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (this.crud.status.add) {
        this.$set(this.form, 'busnessId', this.user.id)
        this.$set(this.form, 'busnessName', this.user.nickName)
      }
    },
    [CRUD.HOOK.beforeSubmit](form) {
      if (this.crud.status.add) {
        const curTime = moment().format('YYYY-MM-DD HH:mm:ss')
        form.form.createTime = curTime
      }
      if (this.crud.status.edit) {
        const curTime = moment().format('YYYY-MM-DD HH:mm:ss')
        form.form.modifyTime = curTime
      }
    },
    updateImg(val) {
      this.$set(this.form, 'url', val)
    },
    open(a, b, c) {
      this.type = a
      this.row = b
      this.dialogVisible = true
      this.timer = new Date()
      this.title = c
    }
  }
}
</script>
<style  lang="scss" scoped>
	.el-form {
		display: flex;
		flex-wrap: wrap;
		.el-form-item {
			width: 49%;
			.el-date-editor {
				width: 100%;
			}
			.el-select {
				width: 100%;
			}
			::v-deep .el-input-number {
				width: 100%;
				.el-input {
					.el-input__inner {
						text-align: left !important;
					}
				}
			}
		}
	}
</style>
